﻿using GalaSoft.MvvmLight;
using Cleverscape.UTorrentClient.WebClient;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Controls;

namespace uTorrentSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class TorrentsViewModel : MyBaseViewModel
    {

        public RelayCommand<Torrent> SelectCommand { get; private set; }

        public RelayCommand UploadCommand { get; set; }

        public RelayCommand<string> AddUrlCommand { get; set; }

        

        /// <summary>
        /// Initializes a new instance of the TorrentsViewModel class.
        /// </summary>
        public TorrentsViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
            SelectCommand = new RelayCommand<Torrent>(t =>
                {
                    Messenger.Default.Send<Torrent>(t, "SetTorrent");
                    //Messenger.Default.Send<string>("TorrentView", "LoadView");
                    
                });

            UploadCommand = new RelayCommand(() =>
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    if (ofd.ShowDialog() == true)
                    {
                        ViewModelLocator.DataService.AddTorrent(ofd.File.OpenRead(), ofd.File.Name);
                    }
                });

            AddUrlCommand = new RelayCommand<string>((url) =>
            {
                Data.AddTorrentFromUrl(url);
                ViewModelLocator.CloseModal();
            });
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}